package org.sosie2.client;

/**
 * author: gabriela 
 */

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringReader;

import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.client.AxisClient;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.configuration.NullProvider;
import org.apache.axis.message.SOAPBody;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.utils.XMLUtils;
import org.apache.log4j.Logger;
import org.sosie2.service.framework.Utils;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;


public class SimpleMuleClient {
//	public static final String endPoint = "http://localhost:8081/demo";
	public static final String endPoint = "http://localhost:8081/demoInMule";
//	public static final String endPoint = "http://localhost:8080/axis/services/DummyService";
//	public  static final String endPoint = "http://137.193.63.202:8081/case4.simple-ws.server1";
	
	public  static final String request1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + 
	"<sosie:client \r\n" + 
	"xmlns:sosie=\"http://www.unibw.de/sosie/test\">\r\n" + 
	"Hello SOS!e 2.0" + 
	"</sosie:client>";
		
	public static final String request2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + 
	"<m:message \r\n" + 
	"xmlns:m=\"http://localhost:8080/axis/services/LDAPAuthenticationService\">\r\n" + 
	"<m:userID>" +
	"Sinbad Marinarul"+
	"</m:userID>"+
	"<m:userPass>"+
	"jRj"+
	"</m:userPass>"+
	"</m:message>" ;
	
	
	static Logger logger = Logger.getLogger(SimpleMuleClient.class);
	public static Message SOAPmessage;
	
	public static void initializeContext() throws Exception {
		logger.info("Initializing Context...");
		
		MessageContext msgContext;
		org.apache.xml.security.Init.init();
		
		AxisClient tmpEngine = new AxisClient(new NullProvider());
		msgContext = new MessageContext(tmpEngine);
		InputStream in = new ByteArrayInputStream(Utils.emptySoapMsg.getBytes());
		Message msg = new Message(in);
		msg.setMessageContext(msgContext);	 
		
		StringReader strReader = new StringReader(request1);		
				
		Document document = XMLUtils.newDocument(new InputSource(strReader));

		// Add request to SOAP body
		SOAPBody body = (SOAPBody) msg.getSOAPBody();					
		body.addDocument(document);
		
		SOAPmessage = msg;
		
		Utils.dumpXML("SOAPMessage:", SOAPmessage.getSOAPEnvelope().getAsDocument(),logger);
		
	}
	
	public static void main(String args[]) throws Exception {
		initializeContext();
				
		Service  service = new Service();
        Call call = (Call) service.createCall();
        call.setTargetEndpointAddress( new java.net.URL(endPoint));
        call.setOperation("s");
        call.setOperationStyle("document");
        call.setOperationUse("literal");
        
		System.out.println("Sent request to Mule entryPoint...");
		Object reply = call.invoke(SOAPmessage);	
		SOAPEnvelope response = (SOAPEnvelope)reply;
		System.out.println("Result=" + response.toString());
	}
	
}


